<template>
    <view class="form-item">
        <view class="form" :class="validFail ? 'valid-error' : ''">
            <image class="icon" :src="iconTypeMap[iconType]"></image>
            <text class="label" v-if="!iconType">{{ label }}</text>
            <input v-if="hidePassword" placeholder-class="placeholder" password
                   :class="iconType ? 'iconForm' : 'labelForm'" @blur="validate"
                   :placeholder="'请输入' + label" v-model="form"></input>
            <input v-else placeholder-class="placeholder"
                   :class="iconType ? 'iconForm' : 'labelForm'" @blur="validate"
                   :placeholder="'请输入' + label" v-model="form"></input>
            <text class="codeBtn" :class="codeLoading ? 'codeLoading' : ''" @click="pushCode"
                  v-if="formType === 'code' || iconType === 'code' || prop === 'code'">{{ codeMsg }}
            </text>
            <image class="viewBtn"
                   src="/static/icons/view.svg"
                   v-if="formType === 'password'"
                   @click="hidePassword = !hidePassword"></image>
        </view>
        <view class="message" v-if="validFail">{{ validMsg }}</view>
    </view>
</template>

<script>
export default {
    props: {
        rules: Object,
        prop: String,
        runValidate: Boolean,
        reset: Number,
        label: {
            type: String,
            default: ''
        },
        iconType: {
            type: String,
            default: ''
        },
        formType: {
            type: String,
            default: ''
        }
    },
    data() {
        return {
            iconTypeMap: {
                code: '/static/icons/code.svg',
                password: '/static/icons/password.svg',
                user: '/static/icons/user.svg'
            },
            form: '',
            codeMsg: '获取验证码',
            codeLoading: false,
            hidePassword: this.formType === 'password',
            validFail: false,
            validMsg: ''
        }
    },
    methods: {
        async pushCode() {
            if (!this.codeLoading) {
                let time = 6
                const timerId = setInterval(() => {
                    time--
                    if (time === 0) {
                        this.codeMsg = '获取验证码'
                        this.codeLoading = false
                        clearInterval(timerId)
                    } else {
                        this.codeMsg = `重新发送(${ time }s)`
                    }
                }, 1000)
                this.codeMsg = `重新发送(${ time }s)`
                uni.showToast({ title: '发送成功' })
            }
            this.codeLoading = true
        },
        validate() {
            const rulesArr = this.rules[this.prop]
            rulesArr.forEach(e => {
                if (e.required) {
                    if (!this.form) {
                        this.validFail = true
                        this.validMsg = e.message
                    } else {
                        this.validFail = false
                        this.validMsg = ''
                    }
                } else if (e.pattern && !this.validFail) {
                    if (e.pattern.test(this.form)) {
                        this.validFail = false
                        this.validMsg = ''
                    } else {
                        this.validFail = true
                        this.validMsg = e.message
                    }
                }
            })
        }
    },
    watch: {
        reset() {
            this.form = ''
        }
    }
}
</script>

<style scoped lang="scss">
.form {
    display: flex;
    align-items: center;
    height: 110rpx;
    border-bottom: 2rpx solid #F2F4F7;
    position: relative;

    text {
        color: #333333;
        font-size: 28rpx;
    }

    image {
        width: 32rpx;
        height: 32rpx;
        position: absolute;
        top: 50%;
        transform: translateY(-50%);
    }

    .icon {
        left: 30rpx;
    }

    .label {
        min-width: 84rpx;
        text-align-last: justify;
    }

    .labelForm {
        margin-left: 30rpx;
    }

    .iconForm {
        margin-left: 92rpx;
    }

    input {
        height: 50rpx;
        color: #333333;
        font-size: 30rpx;
        flex: 1;
    }

    .placeholder {
        color: #D8D8D8;
    }

    .codeBtn {
        margin-left: 30rpx
    }

    .codeLoading {
        color: #7F7F7F;
    }

    .viewBtn {
        right: 30rpx;
    }
}

.valid-error {
    border-bottom: 2rpx solid #CF4444;
}

.message {
    color: #CF4444;
    font-size: 26rpx;
    margin-top: 16rpx;
}
</style>
